package bug
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import bug.buffs.BuffData;
	import bug.buffs.BuffIcon;
	import bug.buffs.BuffManager;
	
	import utils.DisplayObjectUtil;

	public class UIControler
	{
		private var _scene:Scene;
		private var _contain:Sprite;
		private var props:Vector.<BuffIcon> = new Vector.<BuffIcon>();
		private var _power:int=PowerEnum.ME;
		public function UIControler(scene:Scene,contain:Sprite)
		{
			//_scene = scene;
			//_scene.addEventListener(MouseEvent.CLICK,onTouch);
			reset(scene);
			_contain=contain;
			//props=[new UI_Prop(),new UI_Prop(),new UI_Prop(),new UI_Prop];
			init();
		}
		public function reset(scene:Scene):void{
		    _scene=scene;
			_scene.addEventListener(MouseEvent.CLICK,onTouch);
		}
		public function dispose():void{
			_scene.removeEventListener(MouseEvent.CLICK,onTouch);
		}
		private function init():void{
		    var a:Dictionary = BuffManager.getAllBuff();
			var buffIcon:BuffIcon;
			for each (var i:* in a)  
			{
				buffIcon = new BuffIcon(i);
				buffIcon.addEventListener(MouseEvent.CLICK,onUserBuff,false,0,true);
				props.push(buffIcon);
				
			}
		}
		
		private function onUserBuff(e:MouseEvent):void{
			_castle.setBuff(BuffIcon(e.currentTarget).buffData);
		}
		
		
		private var _castle:Castle;
		private function onTouch(e:MouseEvent):void{
			for (i = 0; i < props.length; i++) 
			{
				if(props[i].parent){
					props[i].parent.removeChild(props[i]); 
				}
			} 
			if(_castle) _castle.filters=null ;
			if(e.target is Castle){
				var c:int;
				_castle=e.target as Castle;
				for (var i:int = 0; i < props.length; i++) 
				{
					
					if(_castle._power!=props[i].buffData.power) continue;
					props[i].x=60*c+300;
					props[i].y=500;
					_contain.addChild(props[i]);
					c++;
				}
				e.target.filters=[DisplayObjectUtil.getBitmapFilter(0xffff00,2,2,50)]
			}
		}
		
		
	}
}